package com.game.data;

import com.game.config.Coefficient;
import com.game.model.goods.Goods;
import com.game.model.role.Monster;
import com.game.model.role.Role;
import com.game.model.task.Task;
import com.game.model.task.MainTask;
import com.game.utils.JDBCUtil;
import org.omg.PortableInterceptor.INACTIVE;

import java.io.Serializable;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 **/
public class TaskData implements Serializable {
    public static Task getTaskById(int id) {
        ArrayList<Task> tasks = new ArrayList<>();
        tasks.addAll(tasks());
        for (Task t : tasks) {
            if (t.getId() == id) {
                return t;
            }
        }
        return null;
    }


    public static List<Task> tasks() {
        List<Task> tasks = new ArrayList<>();
        if ("system".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {

            Map<Goods, Integer> goodsMap = new HashMap<>();
            goodsMap.put(GoodsData.getGoods(2000), 1);
            goodsMap.put(GoodsData.getGoods(2001), 1);
            goodsMap.put(GoodsData.getGoods(2002), 1);
            goodsMap.put(GoodsData.getGoods(2003), 1);
            goodsMap.put(GoodsData.getGoods(2004), 1);
            tasks.add(new MainTask(1, "主线任务1", "这是今日的成人大礼，出去这个村，你就是真正的（剧情略..）", null, null, goodsMap, 0));

            Map<Goods, Integer> goodsTask = new HashMap<>();
            goodsTask.put(GoodsData.getGoods(1000), 5);
            HashMap<Goods, Integer> goodsMap1 = new HashMap<>();
            goodsMap.put(GoodsData.getGoods(1), 300);
            tasks.add(new MainTask(2, "搜集蜂蜜", "我需要5罐蜂蜜泡茶喝，能帮我带点蜂蜜吗？（野怪小黄蜂掉落蜂蜜）", null, goodsTask, goodsMap1, 0));
        }

        if ("mysql".equalsIgnoreCase(Coefficient.DATA_SOURCE)) {
            try {
                ResultSet rs = JDBCUtil.executeQuery("select * from main_task");
                while (rs.next()) {
                    MainTask mainTask = new MainTask();
                    mainTask.setId(rs.getInt("id"));
                    mainTask.setName(rs.getString("name"));
                    mainTask.setDecription(rs.getString("description"));
                    mainTask.setUseLevel(rs.getInt("use_level"));
                    //加战斗任务
                    if (rs.getString("fight_task") == null || "".equals(rs.getString("fight_task"))) {
                        mainTask.setFightTask(null);
                    } else {
                        String[] fight_tasks = rs.getString("fight_task").split("-");
                        HashMap<Role, int[]> fight_task_map = new HashMap<>();
                        for (String ft : fight_tasks) {
                            int roleId = Integer.parseInt(ft.split(",")[0]);
                            int count = Integer.parseInt(ft.split(",")[1]);
                            int[] done = {0, count};
                            fight_task_map.put(RoleData.getRoleById(roleId), done);
                        }
                        mainTask.setFightTask(fight_task_map);
                    }
                    //加物品任务
                    if (rs.getString("goods_task") == null || "".equals(rs.getString("goods_task"))) {
                        mainTask.setGoodsTask(null);
                    } else {
                        String[] goods_tasks = rs.getString("goods_task").split("-");
                        HashMap<Goods, Integer> goods_task_map = new HashMap<>();
                        for (String gd : goods_tasks) {
                            int goodsId = Integer.parseInt(gd.split(",")[0]);
                            int count = Integer.parseInt(gd.split(",")[1]);
                            goods_task_map.put(GoodsData.getGoods(goodsId), count);
                        }
                        mainTask.setGoodsTask(goods_task_map);
                    }
                    //加奖励
                    if (rs.getString("rewards") == null || "".equals(rs.getString("rewards"))) {
                        mainTask.setRewards(null);
                    } else {
                        String[] rewards = rs.getString("rewards").split("-");
                        HashMap<Goods, Integer> rewards_map = new HashMap<>();
                        for (String r : rewards) {
                            int goodsId = Integer.parseInt(r.split(",")[0]);
                            int count = Integer.parseInt(r.split(",")[1]);
                            rewards_map.put(GoodsData.getGoods(goodsId), count);
                        }
                        mainTask.setRewards(rewards_map);
                    }
                    tasks.add(mainTask);
                }
                JDBCUtil.close(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return tasks;
    }
}
